
package com.example.lte.po;


import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class SystemInfo {


    public static Map<String, Object> revertSystem() {
        Map<String, Object> map = new HashMap<>();
        map.put( "OsName", SystemInfo.getOsName() );
        map.put( "CpuLoad", SystemInfo.getSystemCpuLoad() );
        map.put( "ProcessCpuLoad", SystemInfo.getProcessCpuLoad() );
        map.put( "TotalMemorySize", SystemInfo.getTotalMemorySize() );
        map.put( "FreePhysicalMemorySize", SystemInfo.getFreePhysicalMemorySize() );
        map.put( "UsedMemory", SystemInfo.getUsedMemory() );
        map.put( "Time",new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( new Date(  ) ));
        return map;
    }


    /**
     * 获取操作系统名称
     *
     * @return
     */

    public static String getOsName() {
        // 操作系统
        String osName = System.getProperty( "os.name" );
        return osName;
    }


    /**
     * 获取系统cpu负载
     *
     * @return
     */

    public static double getSystemCpuLoad() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double SystemCpuLoad = osmxb.getSystemCpuLoad();
        return SystemCpuLoad;
    }


    /**
     * 获取jvm线程负载
     *
     * @return
     */

    public static double getProcessCpuLoad() {
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double ProcessCpuLoad = osmxb.getProcessCpuLoad();
        return ProcessCpuLoad;
    }


    /**
     * 获取总的物理内存
     *
     * @return
     */

    public static long getTotalMemorySize() {
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 总的物理内存
        long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
        return totalMemorySize;
    }


    /**
     * 获取剩余的物理内存
     *
     * @return
     */

    public static long getFreePhysicalMemorySize() {
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        // 剩余的物理内存
        long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
        return freePhysicalMemorySize;
    }


    /**
     * 获取已使用的物理内存
     *
     * @return
     */

    public static long getUsedMemory() {
        int kb = 1024;
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        //已使用的物理内存
        long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb.getFreePhysicalMemorySize()) / kb;
        return usedMemory;
    }
}
